# Contributor: Rasmus Thomsen <oss@cogitri.dev>
# Maintainer: Rasmus Thomsen <oss@cogitri.dev>
pkgname=mozjs68
pkgver=68.11.0
pkgrel=0
pkgdesc="Standalone Mozilla JavaScript engine"
url="https://developer.mozilla.org/en-US/docs/Mozilla/Projects/SpiderMonkey"
arch="x86_64 armv7 armhf aarch64 x86 ppc64le" # limited by rust and cargo
license="MPL-2.0"
depends_dev="
	icu-dev
	nspr-dev
	libffi-dev
	readline-dev
	"
_llvmver=10
makedepends="
	$depends_dev
	zlib-dev
	python2
	python3-dev
	perl
	sed
	autoconf2.13
	linux-headers
	rust
	cargo
	llvm$_llvmver
	clang-dev
	"
subpackages="$pkgname-dev"
source="https://ftp.mozilla.org/pub/firefox/releases/${pkgver}esr/source/firefox-${pkgver}esr.source.tar.xz
	0001-silence-sandbox-violations.patch
	disable-jslint.patch
	fix-musl-build.patch
	fix-rust-target.patch
	fd6847c9416f9eebde636e21d794d25d1be8791d.patch
	"
builddir="$srcdir"/firefox-$pkgver
_builddir="$builddir/js/src"

# secfixes:
#   68.11.0-r0:
#     - CVE-2020-15649
#     - CVE-2020-15650
#     - CVE-2020-15652
#     - CVE-2020-15659
#     - CVE-2020-6463
#     - CVE-2020-6514
#   68.10.0-r0:
#     - CVE-2020-12417
#     - CVE-2020-12418
#     - CVE-2020-12419
#     - CVE-2020-12420
#     - CVE-2020-12421
#   68.9.0-r0:
#     - CVE-2020-12399
#     - CVE-2020-12405
#     - CVE-2020-12406
#     - CVE-2020-12410
#   68.8.0-r0:
#     - CVE-2020-12387
#     - CVE-2020-12388
#     - CVE-2020-12389
#     - CVE-2020-12392
#     - CVE-2020-12393
#     - CVE-2020-12395
#     - CVE-2020-6831
#   68.7.0-r0:
#     - CVE-2020-6821
#     - CVE-2020-6822
#     - CVE-2020-6825
#     - CVE-2020-6819
#     - CVE-2020-6820
#   68.6.0-r0:
#     - CVE-2019-20503
#     - CVE-2020-6805
#     - CVE-2020-6806
#     - CVE-2020-6807
#     - CVE-2020-6811
#     - CVE-2020-6812
#     - CVE-2020-6814
#   60.7.2-r0:
#     - CVE-2019-11708
#     - CVE-2019-11707

build() {
	local _clangver=$(clang --version | awk '$3 == "version" {print $4}')
	if [ "${_clangver#$_llvmver}" = "$_clangver" ]; then
		error "clang version $_clangver does not match llvm version $_llvmver"
		return 1
	fi

	cd "$_builddir"

	export LDFLAGS="$LDFLAGS -Wl,-z,stack-size=1048576"

	# avoid complains about autoconf
	touch configure

	# Fix broken ARM_ARCH check which doesn't work with newer GCCs it seems.
	case "$CTARGET" in
		armv*)
			ARM_ARCH=$(echo ${CTARGET#*armv} | sed "s|-.*||")
			sed -i "s|ARM_ARCH=.*|ARM_ARCH=$ARM_ARCH|" "$builddir"/build/autoconf/arch.m4
			;;
	esac

	export RUST_TARGET="$CTARGET"

	export SHELL=/bin/ash
	export PYTHON=/usr/bin/python2
	./configure --prefix=/usr \
		--with-clang-path=/usr/bin/clang \
		--with-intl-api \
		--with-libclang-path=/usr/lib \
		--with-system-icu \
		--with-system-nspr \
		--with-system-zlib \
		--enable-ctypes \
		--enable-hardening \
		--enable-optimize="$CFLAGS -O2" \
		--enable-readline \
		--enable-release \
		--enable-shared-js \
		--enable-system-ffi \
		--enable-tests \
		--disable-debug \
		--disable-debug-symbols \
		--disable-jemalloc \
		--disable-strip \
		--enable-unaligned-private-values
	make
}

check() {
	cd "$_builddir"
	dist/bin/jsapi-tests \
		--format=none \
		--exclude-random \
		basic
}

package() {
	cd "$_builddir"
	make DESTDIR="$pkgdir" install
	rm -f "$pkgdir"/usr/lib/*.ajs
}

sha512sums="7dcfa4944945bce184b96643a7afbd0cc97c93e4f727695bd5ee1e1745cff89784e68baf109588ef56791211b4b8f5c7c056ae6ac77f54fd00a5af5d5606f23e  firefox-68.11.0esr.source.tar.xz
4d30f4b64b6d9ba03099831aef18f6dffa7f0ff537d71502b8975329c9fa5025f02a43f45cf1311116d917fb507d5961163d659bb4c9f43c22bd61d14d0afb2f  0001-silence-sandbox-violations.patch
6e9e7d811b92e32af29cebec6cb59763004f30b990842b8dd036f971f10d6659e8dbd771f0d61c8a42ea0ac4a6937466c1e6fd450a08efab8a0c7bab4ef3a1ec  disable-jslint.patch
bc91c2fb15eb22acb8acc36d086fb18fbf6f202b4511d138769b5ecaaed4a673349c55f808270c762616fafa42e3b01e74dc0af1dcbeea1289e043926e2750c8  fix-musl-build.patch
c397bd594428b009d1533922a3728a0ec74403714417f4b90c38c1b7751749b0585d48e77c79efa05c6c22a0d9a8ac04d535eb5bb8deb51684852c03c05d94cd  fix-rust-target.patch
60845dcb034b2c4459c30f7d5f25c8176cf42df794e2cc0e86c3e2abb6541c24b962f3a16ca70a288d4d6f377b68d00b2904b22463108559612053d835d9bff1  fd6847c9416f9eebde636e21d794d25d1be8791d.patch"
